package com.buddies.lottery.ui.lottery.select;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ConvertUtils;
import com.buddies.lottery.R;
import com.buddies.lottery.base.list.BaseListActivity;
import com.buddies.lottery.data.bean.Lottery;
import com.buddies.lottery.data.result.PageResult;
import com.buddies.lottery.widget.SpaceItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

public class LotterySelectActivity extends BaseListActivity<Lottery, LotterySelectContract.Presenter> implements LotterySelectContract.View {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lottery_select;
    }

    @Override
    protected void initView() {
        super.initView();

        ((ImageView) findViewById(R.id.iv_up)).setImageResource(R.mipmap.close_black_normal);
        findViewById(R.id.container).setBackgroundResource(R.drawable.btn_white);
        findViewById(R.id.header).setBackgroundResource(R.drawable.btn_white);
        findViewById(R.id.container_root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mColumn = 3;
    }

    @Override
    protected String getHeaderTitle() {
        return "彩种筛选";
    }

    @Override
    protected void initData() {
        super.initData();

        mPresenter = new LotterySelectPresenter(this);

        mRecyclerView.addItemDecoration(new SpaceItemDecoration(ConvertUtils.dp2px(7)));
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Lottery lottery = (Lottery) adapter.getItem(position);

                Intent data = new Intent();
                data.putExtra("lottery", lottery);
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return new LotterySelectAdapter(null);
    }

    @Override
    public void onLoadData(int page) {
        ((LotterySelectContract.Presenter) mPresenter).onLoadData(null, page);
    }

    @Override
    public void onDataLoaded(PageResult<Lottery> response) {
        mSwipeRefreshLayout.setRefreshing(false);
        List<Lottery> list = response.list;
        if (list == null) {
            list = new ArrayList<>();
        }
        Lottery all = new Lottery();
        all.name = "全部";
        list.add(0, all);
        mAdapter.setNewData(list);
        mAdapter.loadMoreEnd(true);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_scale_in, R.anim.anim_scale_out);
    }

    public static void launch(Activity activity, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(activity, LotterySelectActivity.class);
        activity.startActivityForResult(intent, requestCode);
        activity.overridePendingTransition(R.anim.anim_scale_in, R.anim.anim_scale_out);
    }

}
